package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.cache.BloomName;
import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.feign.search.SkuEsFeignClient;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchAttr;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.mapper.*;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.http.nio.pool.NIOConnFactory;
import org.checkerframework.checker.units.qual.A;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> implements SkuInfoService {


    @Autowired
    SkuInfoMapper skuInfoMapper;

    @Autowired
    SkuImageMapper skuImageMapper;

    @Autowired
    SkuAttrValueMapper skuAttrValueMapper;

    @Autowired
    SkuSaleAttrValueMapper skuSaleAttrValueMapper;

    @Autowired
    SkuEsFeignClient skuEsFeignClient;

    @Autowired
    BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    BaseCategory3Mapper baseCategory3Mapper;
    @Autowired
    BaseCategory2Mapper baseCategory2Mapper;
    @Autowired
    BaseCategory1Mapper baseCategory1Mapper;


    @Autowired
    ThreadPoolExecutor executor;

    /*@Autowired
    RedissonClient redissonClient;*/
    @Qualifier(BloomName.SKU)
    @Autowired
    RBloomFilter<Object> skufilter;

    //保存sku信息
    @Transactional
    @Override
    public boolean bigSaveSkuInfo(SkuInfo skuInfo) {


        //1、保存skuinfo基础信息
        int insert = skuInfoMapper.insert(skuInfo);
        Long skuId = skuInfo.getId();
        Long spuId = skuInfo.getSpuId();


        //2、保存sku图片信息
        List<SkuImage> skuImageList = skuInfo.getSkuImageList();

        if (!CollectionUtils.isEmpty(skuImageList)) {
            for (SkuImage skuImage : skuImageList) {
                skuImage.setSkuId(skuId);
                skuImageMapper.insert(skuImage);

            }
        }


        //3、 保存sku平台属性对应的所有值  sku_attr_value
        List<SkuAttrValue> skuAttrValueList = skuInfo.getSkuAttrValueList();

        if (!CollectionUtils.isEmpty(skuAttrValueList)) {
            for (SkuAttrValue skuAttrValue : skuAttrValueList) {
                skuAttrValue.setSkuId(skuId);
                skuAttrValueMapper.insert(skuAttrValue);
            }
        }

        //4、保存sku的销售属性以及值信息 sku_sale_value_id...
        List<SkuSaleAttrValue> skuSaleAttrValueList = skuInfo.getSkuSaleAttrValueList();
        if (!CollectionUtils.isEmpty(skuSaleAttrValueList)) {
            for (SkuSaleAttrValue attrValue : skuSaleAttrValueList) {
                attrValue.setSpuId(spuId);
                attrValue.setSkuId(skuId);
                skuSaleAttrValueMapper.insert(attrValue);

            }
        }
        //保存sku的bloomFilter
        //缓存查数据按照key查：
        String cacheKey = RedisConst.SKUKEY_PREFIX + skuId + RedisConst.SKUKEY_SUFFIX;
        skufilter.add(cacheKey);
        return true;
    }

    /**
     * 事务 + 远程调用；
     * 本地事务：（长事务）
     *    RPC ......
     *  1）、事务里面不要写远程调用，分布式系统不可避免的时候，
     *          远程做成异步编排，不阻塞数据库事务
     *  2）、分布式事务:
     * @param skuId
     * @return
     */
    @Transactional
    @Override
    public boolean onSale(Long skuId) {
        //1、上架修改数据库
        skuInfoMapper.updateSkuSaleStatus(skuId,1);


        /**
         * 以下的所有数据都是全异步
         */
        Goods goods = new Goods();
        //TODO 2、把商品信息同步到搜索引擎，把SKU所有详情全部查出，封装到 goods中
        CompletableFuture<SkuInfo> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1、封装基本信息
            SkuInfo skuInfo = skuInfoMapper.selectById(skuId);
            goods.setId(skuInfo.getId());
            goods.setDefaultImg(skuInfo.getSkuDefaultImg());
            goods.setTitle(skuInfo.getSkuName());
            return skuInfo;
        },executor);

        CompletableFuture<Void> priceFuture = CompletableFuture.runAsync(() -> {
            //2、封装价格
            BigDecimal skuPrice = skuInfoMapper.getSkuPrice(skuId);
            goods.setPrice(skuPrice);
        },executor);

//        goods.setCreateTime(new Date());  //不在这里准备时间，以es的处理为准
        CompletableFuture<Void> baseTrademarkFuture = CompletableFuture.runAsync(() -> {
            //3、封装品牌信息
            BaseTrademark baseTrademark = baseTrademarkMapper.getBaseTradeBySkuId(skuId);
            goods.setTmId(baseTrademark.getId());
            goods.setTmName(baseTrademark.getTmName());
            goods.setTmLogoUrl(baseTrademark.getLogoUrl());
        }, executor);


        CompletableFuture<Void> categoryFuture = skuInfoFuture.thenAcceptAsync((skuInfo) -> {
            //4、封装分类
            BaseCategory3 baseCategory3 = baseCategory3Mapper.selectById(skuInfo.getCategory3Id());
            goods.setCategory3Id(baseCategory3.getId());
            goods.setCategory3Name(baseCategory3.getName());
            BaseCategory2 baseCategory2 = baseCategory2Mapper.selectById(baseCategory3.getCategory2Id());
            goods.setCategory2Id(baseCategory2.getId());
            goods.setCategory2Name(baseCategory2.getName());
            BaseCategory1 baseCategory1 = baseCategory1Mapper.selectById(baseCategory2.getCategory1Id());
            goods.setCategory1Id(baseCategory1.getId());
            goods.setCategory1Name(baseCategory1.getName());
        },executor);

        CompletableFuture<Void> searchAttrsFuture = CompletableFuture.runAsync(() -> {
            //6、当前SKU对应的所有平台属性以及值
            List<SearchAttr> searchAttrs = skuAttrValueMapper.getBaseAttrAndValuesBySkuId(skuId);
            goods.setAttrs(searchAttrs);
        });


        //5、 热度分。每进入商品详情页一次就+1热度
        CompletableFuture<Void> allFuture = CompletableFuture
                .allOf(skuInfoFuture, priceFuture, baseTrademarkFuture, categoryFuture, searchAttrsFuture);

        allFuture.whenCompleteAsync((res,exc)->{
            //远程调用检索服务，保存到es
            skuEsFeignClient.saveSkuToEs(goods);
        },executor);

        return true;
    }

    //503  Service Unavailable
    @Override
    public boolean cancelSale(Long skuId) {
        //1、下架修改数据
        skuInfoMapper.updateSkuSaleStatus(skuId,0);

        CompletableFuture.runAsync(()->{
            //TODO 2、把商品信息从搜索引擎移除
            skuEsFeignClient.deleteSkuFromEs(skuId);
        },executor);

        return true;
    }

    @Override
    public List<Long> getAllId() {

        return skuInfoMapper.getAllIds();


    }
}
